package com.yydm.serviceucenter.controller;


import com.yydm.commonutils.JwtUtils;
import com.yydm.commonutils.R;
import com.yydm.commonutils.ordervo.UcenterVo;
import com.yydm.serviceucenter.entity.Ucenter;
import com.yydm.serviceucenter.entity.vo.RegisterVo;
import com.yydm.serviceucenter.service.UcenterService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yydm
 * @since 2022-07-08
 */
@RestController
@RequestMapping("/uuuu")
//@CrossOrigin  //跨域
public class UcenterController {

    @Autowired
    private UcenterService ucenterService;

    //    登录
    @PostMapping("/login")
    public R loginUser(@RequestBody Ucenter ucenter) {
//        调用service方法实现登录
//        返回toKen值 使用jwt生成
        String toKen = ucenterService.login(ucenter);

        return R.ok().data("toKen", toKen);
    }

    //    注册
    @PostMapping("/register")
    public R registerUser(@RequestBody RegisterVo registerVo) {

        ucenterService.register(registerVo);

        return R.ok();
    }

    //    根据token获取用户信息
    @GetMapping("/getUserInfo")
    public R getMemberInfo(HttpServletRequest request) {
//        调用jwt工具类的方法,根据request对象获取头信息,返回用户id
        Integer memberId = JwtUtils.getMemberIdByJwtToken(request);
//        查询数据库根据用户id获取用户信息
        Ucenter byId = ucenterService.getById(memberId);

        return R.ok().data("userInfo", byId);
    }

    //    根据用户id获取用户信息
    @PostMapping("/getUserInfoOrder/{id}")
    public UcenterVo getUserInfoOrder(@PathVariable Integer id) {
        Ucenter ucenter = ucenterService.getById(id);
//        把ucenter对象里面的值复制给ucenterVo对象
        UcenterVo ucenterVo = new UcenterVo();
        BeanUtils.copyProperties(ucenter, ucenterVo);
        return ucenterVo;
    }

    //    统计某一天注册人数
    @GetMapping("/countRegister/{day}")
    public R registerCount(@PathVariable String day) {

        Integer count = ucenterService.countRegister(day);
        System.out.println(count);
        return R.ok().data("countRegister", count);
    }


}

